using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.BaseManage.Dto;
using ZR.Model.BaseManage;
using ZR.Repository;
using ZR.Service.BaseManage.IBaseManageService;
using Infrastructure;
using Mapster;

namespace ZR.Service.BaseManage
{
    /// <summary>
    /// plc标签信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IBasePlcTagService), ServiceLifetime = LifeTime.Transient)]
    public class BasePlcTagService : BaseService<BasePlcTag>, IBasePlcTagService
    {
        /// <summary>
        /// 查询plc标签信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<BasePlcTagDto> GetPageList(BasePlcTagQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<BasePlcTag, BasePlcTagDto>(parm);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="TagId"></param>
        /// <returns></returns>
        public BasePlcTag GetInfo(string TagId)
        {
            var response = Queryable()
                .Where(x => x.TagId == TagId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加plc标签信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public BasePlcTag AddBasePlcTag(BasePlcTag model)
        {
            if (this.Any(it => it.TagAddress == model.TagAddress && it.PlcCode == model.PlcCode))
            {
                throw new CustomException($"PLC编号[{model.PlcCode}]已包含{model.TagAddress}地址信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改plc标签信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateBasePlcTag(BasePlcTag model)
        {
            if (this.Any(it => it.TagAddress == model.TagAddress && it.PlcCode == model.PlcCode && it.TagId != model.TagId))
            {
                throw new CustomException($"PLC编号[{model.PlcCode}]已包含{model.TagAddress}地址信息");
            }
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<BasePlcTag> QueryExp(BasePlcTagQueryDto parm)
        {
            var predicate = Expressionable.Create<BasePlcTag>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PlcCode), it => it.PlcCode == parm.PlcCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.TagCode), it => it.TagCode == parm.TagCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.TagAddress), it => it.TagAddress == parm.TagAddress);
            return predicate;
        }
    }
}